package com.ldk.framework.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.ldk.base.framework.dao.impl.BaseDAO;
import com.ldk.base.framework.util.BeanUtil;
import com.ldk.framework.dao.IServiceTripDAO;
import com.ldk.framework.entity.ServiceTrip;

/**
* ServiceTripDAO接口实现类
* @author xiangwl
*/
@Repository
public class ServiceTripDAO extends BaseDAO<ServiceTrip> implements IServiceTripDAO {

	@Override
	public List<ServiceTrip> searchTripList(String dbKey,Map<String, Object> parms, Pageable pageable) {
		Assert.notNull(parms);
		try{
			Map<String, Object> otherParms = getPageParms(pageable);
			for (Map.Entry<String, Object> item : otherParms.entrySet()) {
				parms.put(item.getKey(), item.getValue());
			}
			SqlSession sqlSession = getSqlSession(dbKey);
			return sqlSession.selectList(getSqlName("searchTripList"), parms);
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<ServiceTrip> selectListIn(String dbKey, ServiceTrip query, List<String> tripNoList) {
		Assert.notNull(tripNoList);
		try{
			if(tripNoList == null || tripNoList.isEmpty())
				return null;
			Map<String, Object> parms = BeanUtil.toMap(query);
			parms.put("inList", tripNoList);
			
			SqlSession sqlSession = getSqlSession(dbKey);
			return sqlSession.selectList(getSqlName("selectListIn"), parms);
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int updateIn(String dbKey, ServiceTrip entity, List<String> tripNoList) {
		Assert.notNull(entity);
		Assert.notNull(tripNoList);
		try{
			Map<String, Object> parms = BeanUtil.toMap(entity);
			parms.put("inList", tripNoList);
			SqlSession sqlSession = getSqlSession(dbKey);
		    return sqlSession.update(getSqlName("updateIn"), parms);
		    
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return 0;
	}
	
	@Override
	public long getDayCancelCount(String dbKey, ServiceTrip query) {
		Assert.notNull(query);
		try{
			Map<String, Object> parms = BeanUtil.toMap(query);
			SqlSession sqlSession = getSqlSession(dbKey);
		    return sqlSession.selectOne(getSqlName("getDayCancelCount"), parms);
		    
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return 0;
	}
}
